package alva.com.getcamera.utils;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.media.ThumbnailUtils;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.annotation.NonNull;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import alva.com.getcamera.bean.ImageBean;
import alva.com.getcamera.bean.VideoBean;


/**
 * Created by Administrator on 2016/11/17 0017.
 */

public class FileOperateUtil {

    /**
     * named file by time
     *
     * @param fileFormat
     * @return
     */
    public static String setFileNameByTime(@NonNull String fileFormat) {
        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyMMddHHmmss");
        String fileName = format.format(date) + fileFormat;
        return fileName;
    }

    /**
     * set the video saved path that default in SD card.
     *
     * @param directory
     * @param videoFormat
     * @return
     */
    public static File setFileSavedFile(@NonNull String directory, @NonNull String videoFormat) {
        String fileName = FileOperateUtil.setFileNameByTime(videoFormat);
        File fileDir = new File(Environment.getExternalStorageDirectory() + directory);
        if (!fileDir.exists()) {
            fileDir.mkdir();
        }
        File file = new File(fileDir, fileName);
        return file;
    }

    public static void savePicture(@NonNull final byte[] data) {
        new Thread() {
            @Override
            public void run() {
                File picFile = setFileSavedFile("/myPic/", ".jpg");
                FileOutputStream outputStream = null;
                try {
                    outputStream = new FileOutputStream(picFile);
                    Bitmap oldBitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
                    Matrix matrix = new Matrix();
                    matrix.setRotate(90, oldBitmap.getWidth() / 2, oldBitmap.getHeight() / 2);
                    Bitmap newBitmap = Bitmap.createBitmap(oldBitmap, 0, 0, oldBitmap.getWidth(), oldBitmap.getHeight(), matrix, true);
                    newBitmap.compress(Bitmap.CompressFormat.JPEG, 90, outputStream);
                    outputStream.close();
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }.run();
    }

    /**
     * delete file
     *
     * @param path
     */
    public static void remove(final String path) {
        new Thread() {
            @Override
            public void run() {
                File file = new File(path);
                if (file.exists()) {
                    file.delete();
                }
            }
        }.run();
    }

    /**
     * get video from directory that save to ArrayList
     *
     * @param directory
     * @param videoFormat
     * @return
     */
    public static ArrayList<VideoBean> getVideoList(@NonNull final String directory, @NonNull final String videoFormat) {
        final ArrayList<VideoBean> list = new ArrayList<>();
        new Thread() {
            @Override
            public void run() {
                File videoFile = new File(directory);
                if (!videoFile.exists()) {
                    videoFile.mkdir();
                }
                File[] files = videoFile.listFiles();
                for (File file : files) {
                    if (file.getName().endsWith(videoFormat)) {
                        VideoBean videoBean = new VideoBean();
                        videoBean.setPath(file.getPath());
                        videoBean.setVideoName(file.getName());
                        videoBean.setLastModified(getLastModified(file, "yyy/MM/dd  HH:mm:ss"));
                        videoBean.setVideoBitmap(getVideoBitmap(file.getAbsolutePath(), 200, 200));
                        list.add(videoBean);
                    }
                }
            }
        }.run();
        return list;
    }

    public static ArrayList<ImageBean> getPictureList(@NonNull final String directory, @NonNull final String videoFormat) {
        final ArrayList<ImageBean> list = new ArrayList<>();
        new Thread() {
            @Override
            public void run() {
                File imageFile = new File(directory);
                if (!imageFile.exists()) {
                    imageFile.mkdir();
                }
                File[] files = imageFile.listFiles();
                for (File file : files) {
                    if (file.getName().endsWith(videoFormat)) {
                        ImageBean imageBean = new ImageBean();
                        imageBean.setImagePath(file.getAbsolutePath());
                        imageBean.setImageName(file.getName());
                        imageBean.setLastModified(getLastModified(file, "yyy/MM/dd  HH:mm:ss"));
                        list.add(imageBean);
                    }
                }
            }
        }.run();
        return list;
    }

    /**
     * get file the last modified time
     *
     * @param file
     * @param dateFormat
     * @return
     */
    public static String getLastModified(File file, String dateFormat) {
        long time = file.lastModified();
        Date date = new Date(time);
        SimpleDateFormat format = new SimpleDateFormat(dateFormat);
        String lastModified = format.format(date);
        return lastModified;
    }

    /**
     * get video thumbnail
     *
     * @param videoPath
     * @param width
     * @param height
     * @return
     */
    public static Bitmap getVideoBitmap(String videoPath, int width, int height) {
        Bitmap videoThumbnail = ThumbnailUtils.createVideoThumbnail(videoPath, MediaStore.Video.Thumbnails.MINI_KIND);
        Bitmap bitmap = ThumbnailUtils.extractThumbnail(videoThumbnail, width, height);
        return bitmap;
    }
}
